<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ncduc
 * Date: 7/2/13
 * Time: 3:59 PM
 * To change this template use File | Settings | File Templates.
 */
include_once("../libs/setup.php");
include_once("../libs/helper.php");
session_start();

if (!check_user_session()) {
    header("Location: ../signin.php");
    return;
}
assign_home_header($smarty);
$smarty->assign('wwwUrl','../');
function is_valid_employ($id, $cur_userId, mysqli $mysqli)
{
    $query = "SELECT * FROM qua_trinh_cong_tac WHERE Ma=$id";
    $result = $mysqli->query($query);
    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $id = $row['Ma'];
            $userId = $row['MaUser'];
            if ($userId == $cur_userId) {
                return true;
            }
        }

    }
    return false;

}

function load_employ_list(Mysqli $mysqli, $userId, Smarty $smarty)
{
    $query = "SELECT * FROM qua_trinh_cong_tac WHERE MaUser= {$userId}";
    $result = $mysqli->query($query);
    $arr_employ = array();
    if ($result) {
        while ($row = $result->fetch_array(MYSQLI_BOTH)) {
            $arr_employ[] = array('Ma' => $row['Ma'],
                'TenToChuc_vi' => $row['TenToChuc_vi'],
                'TenToChuc_en' => $row['TenToChuc_en'],
                'DiaChi' => $row['DiaChi'],
                'DienThoai' => $row['DienThoai'],
                'TuNgay' => $row['TuNgay'],
                'DenNgay' => $row['DenNgay'],
                'ChucVu_vi' => $row['ChucVu_vi'],
                'ChucVu_en' => $row['ChucVu_en'],
                'ThongTinThem' => $row['ThongTinThem']);
        }
    }
    if ($mysqli->error) {
        system_failed($mysqli->error, $smarty);
        $mysqli->close();
        return false;
    }
    $smarty->assign('items', $arr_employ);
    return true;
}

function get_employ(Mysqli $mysqli, $id)
{
    $query = "SELECT * FROM qua_trinh_cong_tac WHERE Ma={$id}";
    $result = $mysqli->query($query);
    return $result;
}

assign_home_header($smarty);
//get data

$mysqli = connect_database($smarty);
$userId = $_SESSION['ma_user'];
//if page_load
if (empty($_REQUEST['save']) && empty($_REQUEST['next'])) {

    if (!empty($_REQUEST['id']) && !empty($_REQUEST['action'])) {
        if (!is_valid_employ($_REQUEST['id'], $userId, $mysqli)) {
            $message = "Không tìm thấy quá trình công tác";
            $smarty->assign('is_display_error', "block");
            $smarty->assign('error_message', $message);
        } else {
            if ($_REQUEST['action'] == "edit") {


                $result = get_employ($mysqli, $_REQUEST['id']);
                if ($result) {
                    $have_result = false;
                    while ($row = $result->fetch_assoc()) {
                        $have_result = true;
                        $id = $row['Ma'];
                        $employer_vi = $row['TenToChuc_vi'];
                        $employer_en = $row['TenToChuc_en'];
                        $address = $row['DiaChi'];
                        $phone = $row['DienThoai'];
                        $from_date = $row['TuNgay'];
                        $to_date = $row['DenNgay'];
                        $position_vi = $row['ChucVu_vi'];
                        $position_en = $row['ChucVu_en'];
                        $more_info = $row['ThongTinThem'];
                        $smarty->assign('id', $id);
                        $smarty->assign('employer_vi', $employer_vi);
                        $smarty->assign('employer_en', $employer_en);
                        $smarty->assign('address', $address);
                        $smarty->assign('phone', $phone);
                        $smarty->assign('fromDate', $from_date);
                        $smarty->assign('toDate', $to_date);
                        $smarty->assign('position_vi', $position_vi);
                        $smarty->assign('position_en', $position_en);
                        $smarty->assign('more_info', $more_info);
                    }
                    if (!$have_result) {
                        $message = "Không tìm thấy quá trình công tác";
                         $smarty->assign('is_display', "block");
                $smarty->assign('type_message','error');
                $smarty->assign('message', $message);

                    }


                }
                if ($mysqli->error) {
                    system_failed($mysqli->error, $smarty);
                    $mysqli->close();
                    return;
                }
            }
            if ($_REQUEST['action'] == "delete") {

                $id = $_REQUEST['id'];

                $sql = "DELETE FROM qua_trinh_cong_tac WHERE Ma={$id}";
                $mysqli->query($sql);
                if ($mysqli->error) {
                    system_failed($mysqli->error, $smarty);
                    $mysqli->close();
                    return;
                }
            }
        }

    }
    if (!load_employ_list($mysqli, $userId, $smarty)) {
        return;
    }
    $smarty->display('profile/3.tpl');
} else {
    if (isset($_REQUEST['save'])) {
        $id = addslashes($_REQUEST['id']);
        $employer_vi = addslashes($_REQUEST['employer_vi']);
        $employer_en = addslashes($_REQUEST['employer_en']);
        $address = addslashes($_REQUEST['address']);
        $phone = addslashes($_REQUEST['phone']);
        $fromDate = addslashes($_REQUEST['fromDate']);
        $toDate = addslashes($_REQUEST['toDate']);
        $position_vi = addslashes($_REQUEST['position_vi']);
        $position_en = addslashes($_REQUEST['position_en']);
        $more_info = addslashes($_REQUEST['more_info']);
        if (strlen($id) > 0) {

            $sql = "UPDATE qua_trinh_cong_tac SET TenToChuc_vi='{$employer_vi}', TenToChuc_en='{$employer_en}', DiaChi='{$address}', DienThoai='{$phone}', TuNgay=STR_TO_DATE('{$fromDate}','%m-%Y'), DenNgay=STR_TO_DATE('{$toDate}','%m-%Y'), ChucVu_vi='{$position_vi}', ChucVu_en='{$position_en}',ThongTinThem='{$more_info}' WHERE Ma=$id";

        } else {
            $sql = "INSERT INTO qua_trinh_cong_tac (MaUser, TenToChuc_vi, TenToChuc_en, DiaChi,DienThoai, TuNgay, DenNgay, ChucVu_vi, ChucVu_en,ThongTinThem) VALUES ({$userId},'{$employer_vi}','{$employer_en}','{$address}','{$phone}',STR_TO_DATE('{$fromDate}','%m-%Y'),STR_TO_DATE('{$toDate}','%m-%Y'),'{$position_vi}','{$position_en}','{$more_info}')";
            $id = $mysqli->insert_id;

        }
        $result = $mysqli->query($sql);
        if ($mysqli->error) {
            system_failed($mysqli->error . "sql: " . $sql, $smarty);
            $mysqli->close();
            return;
        }

        if (!load_employ_list($mysqli, $userId, $smarty)) {
            return;
        }
        $smarty->display('profile/3.tpl');
        $mysqli->close();
    } else //Next button clicked
    {
        header("Location: 4.php");
    }

}


